<?php


/**
 * Class for NotEarlyThanToday Validation
 *
 * @category   Zend
 * @uses       Zend_Validate_Abstract
 * @package    Invoices
 * @subpackage Zend_Validate
 * @copyright  Copyright (c) 2009 East Hui Van Business Consulting Co., Ltd. (http://www.easthv.com)
 */
class Zend_Validate_NotEarlyThanToday extends Zend_Validate_Abstract
{
    /**
     * Validation failure message key
     */
	const NOT_DATE = 'notLater';

    /** 
     * @var String 
     */
	protected $_format;


    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::NOT_DATE    => "input date can not early than today"
	);

    /** 
     * Set the date format. 
     * 
     * @param string $format
     * @return Zend_Validate_NotEarlyThanToday Provides a fluent interface 
     */ 
    public function setFormat($format) 
    { 
        $this->_format = $format; 
 
        return $this; 
    } 

    /** 
     * Sets validator options 
     * 
     * @param  string $param
     * @return void 
     */
    public function __construct($param = null)
	{
		$config = Zend_Registry::get('config');
		$this->setFormat($config['system']['date']['formatUser']);

    }



    /** 
     * Defined by Zend_Validate_Interface 
     * 
     * Returns true if and only if the input date is not early than today
     * 
	 * @param string $value
	 *
     * @return boolean 
     */ 
    public function isValid($value)
	{

		$this->_setValue($value);

		$date = new Zend_Date($value,$this->_format);

		Zend_Invoice_Date::setUserTimeZone();
		$today = Zend_Invoice_Date::getToday();
		
		if ($date->isEarlier($today)) {
            $this->_error(self::NOT_DATE);
            return false;
        }

        return true;
    }

}
